package com.adv.ems.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.adv.ems.db.mapper.EnergyDataCumulateMapper;
import com.adv.ems.db.entity.EnergyDataCumulateDO;
import com.adv.ems.manager.EnergyDataCumulateManager;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class EnergyDataCumulateManagerImpl extends ServiceImpl<EnergyDataCumulateMapper, EnergyDataCumulateDO> implements EnergyDataCumulateManager {
    @Override
    public List<EnergyDataCumulateDO> getByPointIdAndColDate(Long collectPointId, Date colDate) {
        LambdaQueryWrapper<EnergyDataCumulateDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EnergyDataCumulateDO::getCollectPointId, collectPointId);
        wrapper.eq(EnergyDataCumulateDO::getColDate, colDate);
        wrapper.orderByDesc(EnergyDataCumulateDO::getId);
        return list(wrapper);
    }

    @Override
    public Boolean updateColData(Long id, Date colDate, Double value) {
        LambdaQueryWrapper<EnergyDataCumulateDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EnergyDataCumulateDO::getId, id);
        wrapper.eq(EnergyDataCumulateDO::getColDate, colDate);
        EnergyDataCumulateDO record = new EnergyDataCumulateDO();
        record.setOriValue(value);
        return update(record, wrapper);
    }

    @Override
    public Boolean deleteColData(Long id, Date colDate) {
        LambdaQueryWrapper<EnergyDataCumulateDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EnergyDataCumulateDO::getId, id);
        wrapper.eq(EnergyDataCumulateDO::getColDate, colDate);
        return remove(wrapper);
    }

    @Override
    public boolean insertData(EnergyDataCumulateDO record) {
       return save(record);
    }

    @Override
    public List<EnergyDataCumulateDO> getAllDataByPonitTime(Long pointId, Date start, Date end) {
        LambdaQueryWrapper<EnergyDataCumulateDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EnergyDataCumulateDO::getCollectPointId, pointId);
        wrapper.ge(EnergyDataCumulateDO::getColDate, start);
        wrapper.le(EnergyDataCumulateDO::getColDate, end);
        wrapper.orderByAsc(EnergyDataCumulateDO::getColDate);
        return list(wrapper);
    }

    @Override
    public EnergyDataCumulateDO getTimeBeforeOneData(Long pointId, Date start) {
        LambdaQueryWrapper<EnergyDataCumulateDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EnergyDataCumulateDO::getCollectPointId, pointId);
        wrapper.lt(EnergyDataCumulateDO::getColDate, start);
        wrapper.orderByDesc(EnergyDataCumulateDO::getColDate);
        wrapper.last("limit 1");
        List<EnergyDataCumulateDO> list = list(wrapper);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public void updateIncreaseValue(Long id, Date colDate, Double increaseRealValue, Double increaseStandValue) {
        LambdaQueryWrapper<EnergyDataCumulateDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EnergyDataCumulateDO::getId, id);
        wrapper.eq(EnergyDataCumulateDO::getColDate, colDate);
        EnergyDataCumulateDO record = new EnergyDataCumulateDO();
        record.setIncreaseRealValue(increaseRealValue);
        record.setIncreaseStandValue(increaseStandValue);
        update(record, wrapper);
    }

    @Override
    public List<EnergyDataCumulateDO> selectData(Date startDate, Date endDate, List<Long> pointIds) {
        LambdaQueryWrapper<EnergyDataCumulateDO> wrapper = new LambdaQueryWrapper<>();
        if (CollectionUtils.isEmpty(pointIds)) {
            return new ArrayList<>();
        }
        wrapper.in(EnergyDataCumulateDO::getCollectPointId, pointIds);
        wrapper.ge(EnergyDataCumulateDO::getColDate, startDate);
        wrapper.le(EnergyDataCumulateDO::getColDate, endDate);
        return list(wrapper);
    }
}
